﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ShoppingOnline.Configuration;
using Microsoft.EntityFrameworkCore;
using ShoppingOnline.Model;
using ShoppingOnline.Service;

// For more information on enabling Web API for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860

namespace ShoppingOnline.Controllers
{
    [Route("[controller]")]
    public class ControllerBase<T>: Controller where T:class,IModel
    {
        private ServiceDbcontext _services { get; set; }
        public ControllerBase(ServiceDbcontext services)
        {
            _services = services;
        }

        
        [HttpGet()]
        public async Task<IActionResult> GetAll()
        {
            //var all = await _services.DbContext.Set<T>().Where(t => t.Flag == 0).ToListAsync();
            return Ok(await _services.DbContext.Set<T>().Where(t => t.Flag == 0).ToListAsync());
        }

        [HttpGet("{id:int}")]
        public async Task<IActionResult> GetById(int id)
        {
            var result = await _services.DbContext.Set<T>().SingleOrDefaultAsync(t => t.ID== id && t.Flag == 0);
            return Ok(result);
        }

        
        //需要加入验证
        [HttpPost()]
        public async Task<IActionResult> AddData([FromBody]T model)
        {
            if (model == null)
                return null;
            //则用户状态为0，商品上架状态？需要测试
            model.Flag = 0;
            _services.DbContext.Set<T>().Add(model);
            var newmodel = await _services.DbContext.SaveChangesAsync();
            return Ok(newmodel.ToString());

        }

        //X
        [HttpPut()]
        public async Task<IActionResult> ChangeData([FromBody]T model)
        {
            if (model == null)
                return null;
            var oldmodel = await _services.DbContext.Set<T>().FirstOrDefaultAsync(t => t.ID== model.ID);
            ChangeModel(oldmodel, model);
            _services.DbContext.Update(model);
            var newmodel =await _services.DbContext.SaveChangesAsync();
            return Ok(newmodel.ToString());
        }

        protected virtual T ChangeModel(T oldmodel, T model)//一定不能是PUBLIC
        {
            return model;
        }

        [HttpPut("Delete/{id:int}")]
        public async Task<IActionResult> DeleteModel(int id)
        {
            var model = await _services.DbContext.Set<T>().FirstOrDefaultAsync(t => t.ID== id && t.Flag == 0);
            model.Flag = 1;
            _services.DbContext.Set<T>().Update(model);
            var deletemodel = _services.DbContext.SaveChangesAsync();
            return Ok(deletemodel.ToString());
        }
        //// GET: api/values
        //[HttpGet]
        //public IEnumerable<string> Get()
        //{
        //    return new string[] { "value1", "value2" };
        //}

        //// GET api/values/5
        //[HttpGet("{id}")]
        //public string Get(int id)
        //{
        //    return "value";
        //}

        //// POST api/values
        //[HttpPost]
        //public void Post([FromBody]string value)
        //{
        //}

        //// PUT api/values/5
        //[HttpPut("{id}")]
        //public void Put(int id, [FromBody]string value)
        //{
        //}

        //// DELETE api/values/5
        //[HttpDelete("{id}")]
        //public void Delete(int id)
        //{
        //}
    }
}
